const http = require('http');
const url = require('url');
const querystring = require('querystring');


const server = http.createServer((req, res) => {

  let rb = url.parse(req.url, true);
  console.log(rb);
  const { pathname, query } = rb;


  if (pathname === '/') {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(` 
      <html>
        <head>
          <title>Node.js HTTP Server</title>
        </head>
        <body>
          <h1>Welcome to Node.js HTTP Server</h1>
          <p>This is a simple HTTP server that listens on port 3000 and returns "Hello World" as the response.</p>
          <p>To test the server, you can use a web browser or a tool like curl to send HTTP requests to the server.</p>
          <p>For example, to get the "Hello World" response, you can use the following command:</p>
          <pre>
            curl http://localhost:3000
          </pre>
          <p>To test the server's URL parameters, you can use the following command:</p>
          <pre>
            curl http://localhost:3000/url?name=John&age=30
          </pre>
          <p>This will return a JSON object with the query parameters.</p>
          <p>To test the server's login functionality, you can use the following command:</p>
          <pre>
            curl http://localhost:3000/login
          </pre>
          <p>This will return a login page. You can then submit the login form with the username "admin" and the password "password" to log in.</p>
        </body>
      </html>
    `);
  } else if (pathname === '/url') {

    res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
    res.end(JSON.stringify(query));

  } else if (pathname === '/login' && req.method === 'GET') {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(`
      <html>
        <head>
          <title>Login Page</title>
        </head>
        <body>
          <form method="POST" action="/login">
            <label for="username">Username:</label>
            <input type="text" id="username" name="username"><br><br>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password"><br><br>
            <input type="submit" value="Login">
          </form>
        </body>
      </html>
    `);
  } else if (pathname === '/login' && req.method === 'POST') {
    let body = '';
    req.on('data', chunk => {
      body += chunk.toString();
    });
    req.on('end', () => {
      console.log(body);
      const { username, password } = querystring.parse(body);
      console.log(`Username: ${username}, Password: ${password}`);
      if (username === 'admin' && password === 'password') {
        res.writeHead(302, { 'Location': '/' });
        res.end();
      } else {
        res.writeHead(401, { 'Content-Type': 'text/plain' });
        res.end('Invalid username or password');
      }
    });

  } else {
    res.writeHead(302, { 'Location': '/login' });
    res.end();
  }
});


const port = 3000;

server.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/login`);
});

// This is a simple HTTP server that listens on port 3000 and returns "Hello World" as the response.    